/**
 * Created by ivancai on 2017/7/24.
 */


//提示框
function TipLayer (sCont,sStyle,fn,T) {
    layer.open({
        content: sCont,
        style:sStyle != 'no' ? sStyle : 'background-color:rgba(255,255,255,1); color:#252525; border:none; border-radius: 5px;',
        scrollbar: false,
        time: T || 2,
        shadeClose: true,
        success:function() {if(fn)fn();}
    });
}


var buyCount = 1, curPrice = $('#price').text(), originPrice = curPrice, curProductId = $('#proId').val(),
    curStore = $('#store').text()
// setDefaultUI()
function onChooseItem(productId, name, store, price, imgPath, li) {
    originPrice = curPrice = price, buyCount = 1, curProductId = productId, curStore = store
    if($(li).hasClass('noStore')) {
        TipLayer("该商品已经没有库存啦~");
        return;
    }
    updateBuyCountUI()
    changeCurStatus(name, store, price, imgPath)
    $('.productItem').removeClass("on")
    $(li).addClass("on")
}
function changeCurStatus(name, store, price, imgPath) {
    $('#proName').text(name)
    $('#store').text(store)
    $('#price').text(price)
    if( imgPath != ''){
        $('#proImage').attr("src", imgPath);
    }

}
function setDefaultUI() {
    $($('.productItem')[0]).addClass("on")
}
function onPlus() {
    if (buyCount < 99) {
        if (buyCount >= curStore) {
            return
        }
        buyCount++
        curPrice = (buyCount * originPrice).toFixed(2)
        updateBuyCountUI()
    }
}
function onReduce() {
    if (buyCount <= 1)
        return
    buyCount--
    curPrice = (buyCount * originPrice).toFixed(2)
    updateBuyCountUI()
}
function updateBuyCountUI() {
    $('#buyCount').val(buyCount)
    $('#price').text(curPrice)
}
$("#buyCount").bind('input porpertychange', function () {
    var input = $("#buyCount").val()
    if (input < 1) {
        $("#buyCount").val(1)
    }
    if (input >= parseInt(curStore)) {
        $("#buyCount").val(curStore)
        input = $("#buyCount").val()
        buyCount = input, curPrice = (buyCount * originPrice).toFixed(2)
        updateBuyCountUI()
        return
    }
    if (input > 99) {
        $("#buyCount").val(99)
    }
    input = $("#buyCount").val()
    buyCount = input, curPrice = (buyCount * originPrice).toFixed(2)
    updateBuyCountUI()
});
startCounter2()
startCounter3()
startCounter4()
startCounterForOrderList();
function startCounter2() {
    var createTime = $('#createTime').val()
    createTime = Math.round(createTime / 1000)
    setInterval(function () {
        var currentTime = Math.round(new Date().getTime() / 1000), endTime = createTime + 24 * 3600
        var activityEndTimeStamp = $('#activityEndTimeStamp').val()
        activityEndTimeStamp = Math.round(activityEndTimeStamp / 1000)
        if (endTime > activityEndTimeStamp) //如果普通情况结束时间大于活动结束时间,说明该团在24小时之前就已经结束,取活动结束时间为最终时间
            endTime = activityEndTimeStamp
        // console.log(endTime);
        // console.log(currentTime);
        var hours = timeStamp2Datetime(endTime - currentTime, 'hours'),
            minutes = timeStamp2Datetime(endTime - currentTime, 'minutes'),
            seconds = timeStamp2Datetime(endTime - currentTime, 'seconds')
        $('#hours').text(hours)
        $('#minutes').text(minutes)
        $('#seconds').text(seconds)
        if (hours == '00' && minutes == '00' && seconds == '00') {
            $('#btn_join').css('display', 'none')
            $('.height100').css('height', 'auto')
        }
    }, 300)
}
function startCounter3() {
    var createTime = $('#createTime').val()
    createTime = Math.round(createTime / 1000)
    setInterval(function () {
        var currentTime = Math.round(new Date().getTime() / 1000), endTime = createTime + 24 * 3600
        var activityEndTimeStamp = $('#activityEndTimeStamp').val()
        activityEndTimeStamp = Math.round(activityEndTimeStamp / 1000)
        if (endTime > activityEndTimeStamp) //如果普通情况结束时间大于活动结束时间,说明该团在24小时之前就已经结束,取活动结束时间为最终时间
            endTime = activityEndTimeStamp
        var hours = timeStamp2Datetime(endTime - currentTime, 'hours'),
            minutes = timeStamp2Datetime(endTime - currentTime, 'minutes'),
            seconds = timeStamp2Datetime(endTime - currentTime, 'seconds')
        $('#hours2').text(hours)
        $('#minutes2').text(minutes)
        $('#seconds2').text(seconds)
        if (hours == '00' && minutes == '00' && seconds == '00') {
            $('#btn_join').css('display', 'none')
            $('.height100').css('height', 'auto')
        }
    }, 300)
}
//拼单订单列表倒计时
function startCounterForOrderList() {
    var createTimes = [];
    var activityEndTimeStamps = [];
    var endTimes = [];
    //var currentTimes = [];
    var $jsTime = $('.js_orderListTime')

        $jsTime.each(function (i) {
            createTimes[i] = Math.round( $jsTime.eq(i).find(".createTime").val() / 1000);
            activityEndTimeStamps[i] =Math.round( $jsTime.eq(i).find(".activityEndTimeStamp").val() / 1000) ;
            endTimes[i] = createTimes[i]+24 * 3600;
            if( endTimes[i] > activityEndTimeStamps[i] ){
                endTimes[i] = activityEndTimeStamps[i];//如果普通情况结束时间大于活动结束时间,说明该团在24小时之前就已经结束,取活动结束时间为最终时间
            }
        });
        setInterval(function () {
            var currentTime = Math.round(new Date().getTime() / 1000);
            for (var i=0; i<$jsTime.length; i++) {
                var hours = timeStamp2Datetime(endTimes[i] - currentTime, 'hours');
                var minutes = timeStamp2Datetime(endTimes[i] - currentTime, 'minutes');
                var seconds = timeStamp2Datetime(endTimes[i] - currentTime, 'seconds');

                if (hours == '00' && minutes == '00' && seconds == '00') {
                    $jsTime.eq(i).hide();
                }

                $jsTime.eq(i).find('.hours').text(hours);
                $jsTime.eq(i).find('.minutes').text(minutes);
                $jsTime.eq(i).find('.seconds').text(seconds);
            }


    }, 1000)
}

//有天数的
// var activityEndTimeStampNew1;
function startCounter4() {
    // var createTime = $('#createTime').val()
    // createTime = Math.round(createTime / 1000)
    var activityEndTimeStampNew1 = $('#activityEndTimeStampNew').val()
    activityEndTimeStampNew1 = Math.round(activityEndTimeStampNew1 / 1000)
    var timeCountDown = setInterval(function () {
        // var currentTime = Math.round(new Date().getTime() / 1000), endTime = createTime + 24 * 3600

        //console.log(activityEndTimeStampNew1)
        if (activityEndTimeStampNew1 < 0) //活动时间少于0就让它等于0
            activityEndTimeStampNew1 = 0;
        var days= timeStamp3Datetime(activityEndTimeStampNew1, 'days'),
            hours = timeStamp3Datetime(activityEndTimeStampNew1, 'hours'),
            minutes = timeStamp3Datetime(activityEndTimeStampNew1, 'minutes'),
            seconds = timeStamp3Datetime(activityEndTimeStampNew1, 'seconds')
        $('#daysNew').text(days)
        $('#hoursNew').text(hours)
        $('#minutesNew').text(minutes)
        $('#secondsNew').text(seconds)
        if( days == '00'){
            $('#jsDay').css('display', 'none');
        }
        if (hours == '00' && minutes == '00' && seconds == '00') {
            // $('#btn_join').css('display', 'none')
            // $('.height100').css('height', 'auto')
        }
        activityEndTimeStampNew1--
    }, 1000)
}
function startCounter() {
    var remainClass = $('.remainTime'), times = []
    for (var i = 0; i < remainClass.length; i++) {
        var miliTimeStamp = $(remainClass[i]).text()
        var timeStamp = Math.round(miliTimeStamp / 1000)
        times.push(timeStamp)
        setInterval(function () {
            times.forEach(function (createTime, index) {
                var currentTime = Math.round(new Date().getTime() / 1000), endTime = createTime + 24 * 3600
                var dateTime = timeStamp2Datetime(endTime - currentTime)
                $($('.remainTime')[index]).text(dateTime)
                createTime--
            })
        }, 200)
    }
}

function timeStamp2Datetime(spaceTimeStamp, type) {
    if (spaceTimeStamp <= 0)
        return '00'
    var hours = Math.floor(spaceTimeStamp / 3600), minutes = Math.floor((spaceTimeStamp - 3600 * hours) / 60),
        seconds = spaceTimeStamp - hours * 3600 - minutes * 60;
    if (hours < 10)
        hours = '0' + hours;
    if (minutes < 10)
        minutes = '0' + minutes
    if (seconds < 10)
        seconds = '0' + seconds
    switch (type) {
        case 'hours':
            return hours
        case "minutes":
            return minutes
        case "seconds":
            return seconds
    }
}




//有天数的
function timeStamp3Datetime(spaceTimeStamp, type) {
    if (spaceTimeStamp <= 0)
        return '00'
    var hours = Math.floor(spaceTimeStamp / 3600), minutes = Math.floor((spaceTimeStamp - 3600 * hours) / 60),
        seconds = spaceTimeStamp - hours * 3600 - minutes * 60;
    if(hours>24){
        var days = Math.floor(hours/24);
        hours = hours - (days*24);
        if(days < 10){
            days = '0' + days;
        }
    }else {
        var days = '00'
    }
    if (hours < 10)
        hours = '0' + hours;
    if (minutes < 10)
        minutes = '0' + minutes
    if (seconds < 10)
        seconds = '0' + seconds
    switch (type) {
        case 'days':
            return days
        case 'hours':
            return hours
        case "minutes":
            return minutes
        case "seconds":
            return seconds
    }
}
function submitPintuanOrder() {
    if ( $('.js_pintuanSizeList').find('li.on').length == 0 ) {
        TipLayer("请选择商品规格！");
        return;
    }else{
        var productAndquantity = curProductId + ',' + buyCount
        var activityId = $('#activityId').val(), recordId = $('#recordId').val()
        window.location.href = '/shop/pay/buynow?productIdAndquantity=' + productAndquantity + "&isGroupBuy=yes&groupBuyType=GroupBuyTypeJoin&groupBuyActivityId=" + activityId
            + "&groupBuyRecordId=" + recordId
    }
}